 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : WebQQCore
 * Package  : iqq.im.protocol
 * File     : CheckVerifyAction.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2012-9-1
 * License  : Apache License 2.0 
 */
package com.windmill.action;

import iqq.im.QQActionListener;
import iqq.im.QQException.QQErrorCode;
import iqq.im.action.AbstractHttpAction;
import iqq.im.core.QQService;
import iqq.im.event.QQActionEvent;
import iqq.im.http.QQHttpRequest;
import iqq.im.http.QQHttpResponse;
import iqq.im.service.HttpService;
import iqq.im.util.QQEncryptor;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.windmill.bean.CompleteTheme;
import com.windmill.core.MargicCardClient;
import com.windmill.util.XMLUtils;

/**
 *获取魔卡卡片V3版本所有信息
 *
 */
public class GetCardCollectionAction extends AbstractHttpAction {
	private MargicCardClient client;
	private String qq;
	public GetCardCollectionAction(String qq, MargicCardClient context, QQActionListener listener) {
		super(context, listener);
		this.client = context;
		this.qq = qq;
	}

	@Override
	public QQHttpRequest buildRequest() {
		HttpService httpService = (HttpService) getContext().getSerivce(QQService.Type.HTTP);		
		String gtk = QQEncryptor.gtk(httpService.getCookie("skey",null).getValue());
		QQHttpRequest req = createHttpRequest("POST","http://card.show.qq.com/cgi-bin/card_collection_get?g_tk="+gtk);
		req.addHeader("Content-Type", "application/x-www-form-urlencoded");
		req.removeHeader("Referer");
		req.addPostValue("uin", qq);
		return req;
	}	

	@Override
	public void onHttpStatusOK(QQHttpResponse response) {
		try {
			JSONArray jsonArray = new JSONArray(XMLUtils.parseList(new StringReader(response.getResponseString()),"Node"));
			List<CompleteTheme> list =  parseCompleteList(jsonArray);				
			notifyActionEvent(QQActionEvent.Type.EVT_OK,list);
		} catch (Exception e) {
			System.out.println("解释CardCollection 错误。");
			notifyActionEvent(QQActionEvent.Type.EVT_ERROR, QQErrorCode.UNEXPECTED_RESPONSE);
		}        
	}

	@SuppressWarnings("rawtypes")
	private List<CompleteTheme> parseCompleteList(JSONArray jsonArray) throws JSONException {
		List<CompleteTheme> list = new ArrayList<CompleteTheme>();
		for(int i=0;i<jsonArray.length();i++){
			Map m = (Map) jsonArray.get(i);
			JSONObject obj = new JSONObject(m);
			CompleteTheme complete = new CompleteTheme();
			complete.setTheme(client.getCardStore().getThemeById(obj.getInt("theme_id")));
			complete.setType(obj.getInt("type"));
			complete.setCompletetime(obj.getLong("completetime"));
			complete.setGift(client.getCardStore().getGiftByThemeIdAndSzType(obj.getInt("theme_id"),obj.getString("gift")));
			list.add(complete);
		}
		return list;
	}



}
